import 'package:flutter/material.dart';
import 'package:flutterdemo/0demo/provider/inherited_widget_test/inherited_context.dart';

import 'inherited_test_model.dart';

class InheritedWidgetTestPage extends StatefulWidget {
  @override
  _InheritedWidgetTestPageState createState() => _InheritedWidgetTestPageState();
}

class _InheritedWidgetTestPageState extends State<InheritedWidgetTestPage> {
  InheritedTestModel inheritedTestModel;

  @override
  void initState() {
    inheritedTestModel = InheritedTestModel(100);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InheritedContext(
        inheritedTestModel,
        _incrementCount,
        _reduceCount,
        Scaffold(
          appBar: AppBar(
            title: Text("InheritedWidgetTestPage"),
          ),
          body: Column(
            children: [
              SizedBox(height: 20),
              TestWidgetA(),
              SizedBox(height: 20),
              TestWidgetB(),
              SizedBox(height: 20),
              TestWidgetC(),
            ],
          ),
        ));
  }

  _incrementCount() {
    setState(() {
      // inheritedTestModel = new InheritedTestModel(inheritedTestModel.count + 1);
    });
  }

  _reduceCount() {
    setState(() {
      // inheritedTestModel = new InheritedTestModel(inheritedTestModel.count - 1);
    });
  }
}

class TestWidgetA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);

    // final inheritedTestModel = inheritedContext.inheritedModel;
    //
    // print('TestWidgetA 中count的值:  ${inheritedTestModel.count}');
    print("build +");

    return new Padding(
      padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
      child: new RaisedButton(textColor: Colors.black, child: new Text('+'), onPressed: inheritedContext.increment),
    );
  }
}

class TestWidgetB extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);

    final inheritedTestModel = inheritedContext.inheritedModel;

    print('TestWidgetB 中count的值:  ${inheritedTestModel.count}');

    return new Padding(
      padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
      child: new Text(
        '当前count:${inheritedTestModel.count}',
        style: new TextStyle(fontSize: 20.0),
      ),
    );
  }
}

class TestWidgetC extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);

    // final inheritedTestModel = inheritedContext.inheritedModel;
    //
    // print('TestWidgetC 中count的值:  ${inheritedTestModel.count}');
    print("build -");
    return new Padding(
      padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
      child: new RaisedButton(textColor: Colors.black, child: new Text('-'), onPressed: inheritedContext.reduce),
    );
  }
}
